package com.jigsawapp.movies.ui;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewConfiguration;

import com.jigsawapp.movies.AppManager;
import com.jigsawapp.movies.R;
import com.jigsawapp.movies.common.StringUtils;

import java.lang.reflect.Field;


public abstract class BaseActivity extends FragmentActivity {

    // 是否可以返回
    protected static boolean isCanBack;

//    protected ActionBar mActionBar;

    protected String mTitle;

    protected String mSubTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initActionBar();
        setOverflowButton();
        getActionBar().setDisplayShowHomeEnabled(false);
        //将activity加入到AppManager堆栈中
        AppManager.getAppManager().addActivity(this);
    }

    @Override
    public boolean onNavigateUp() {
        finish();
        return super.onNavigateUp();
    }

    // 关闭该Activity
//    @Override
//    public boolean onSupportNavigateUp() {
//        finish();
//        return super.onSupportNavigateUp();
//    }

    // 初始化ActionBar
    private void initActionBar() {
//        mActionBar = getSupportActionBar();
//        int flags = ActionBar.DISPLAY_HOME_AS_UP;
//        int change = mActionBar.getDisplayOptions() ^ flags;
//        // 设置返回的图标
//        mActionBar.setDisplayOptions(change, flags);
//        if (mTitle != null && !StringUtils.isEmpty(mTitle)) {
//            mActionBar.setTitle(mTitle);
//        }
//        if (mSubTitle != null && !StringUtils.isEmpty(mSubTitle)) {
//            mActionBar.setSubtitle(mSubTitle);
//        }
    }

//    public void setActionBarTitle(String title) {
//        mActionBar.setTitle(title);
//    }

//    public void setActionBarSubTitle(String subTitle) {
//        mActionBar.setSubtitle(subTitle);
//    }

    protected <T extends View> T findView(int id) {
        return (T) findViewById(id);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void setOverflowButton() {
        ViewConfiguration config = ViewConfiguration.get(this);
        try {
            Field menuKey = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            menuKey.setAccessible(true);
            menuKey.setBoolean(config, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().removeActivity(this);
    }
}
